<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Action
 *
 * @author wlf
 */
if(!defined('TinyFW')) exit('Access denied!');

class tfAction {
   //前置處理
    public function preAction() {
       //load js
       $this->_generate_js();
       $this->_generate_css();
       $this->_generate_title();

    }

    private function _generate_js(){
        $tf_js = '';
        //print_r(tfConfig::getConfig('js'));
        foreach(tfConfig::getConfig('js') as $js) {
            $tf_js .= "<script src='" . $js . "' type='text/javascript'></script>\r\n";
        }
        
        $this->assign('tf_js',$tf_js);
    }

    private function _generate_css(){
        $tf_css = '';
        foreach(tfConfig::getConfig('css') as $css) {
            $tf_css .= "<link rel='stylesheet' type='text/css' href='". $css ."' />\r\n";
        }
        $this->assign('tf_css',$tf_css);
    }

    private function _generate_title(){
        $tf_title = tfConfig::getConfig('app_name') ;
        if(tfConfig::getConfig('mod_name')) {
            $tf_title .= " - " . tfConfig::getConfig('mod_name');
        }
        $this->assign('tf_title', $tf_title);
    }

    //後置處理
    public function postAction(){
        
    }
    
    public function indexAction() {
        trigger_error("Cannot find your Index Action, Please define it first!", E_USER_ERROR);
    }

    
    public function __call($method_name, $params) {
        //if the method is existed in template , call it
        if(method_exists(tfTemplate::getTpl(), $method_name)) {
            //echo " smarty method exists";
            //print_r($params);
            return call_user_method_array($method_name, tfTemplate::getTpl() , $params);
        } else {
            trigger_error("The method  '" . $method_name . "' does not exists!", E_USER_ERROR);
        }

    }

    public function run() {

         //echo "action run<br>";;
        /*if( strtoupper($_SERVER["REQUEST_METHOD"]) == "POST") {
            header("pragma: no-cache");
            header("Cache-Control: no-cache, must-revalidate");
            header("expires: 0");
            ob_start();
        }*/
         $this->preAction();
         //$this->render();
         $action_name = tfUri::getActionName();
         //echo $action_name."<br>";
         if(method_exists('Action',$action_name)) {
             //echo "method exists<br>";
             $this->$action_name();
         } elseif(method_exists("Action","indexAction")) {
             $this->indexAction();
         }
         
         $this->postAction();
         
    }

}
?>
